//
//  HSCollectionVC.m
//  HolyShit
//
//  Created by Robin on 2017/3/30.
//  Copyright © 2017年 HolyShit. All rights reserved.
//

#import "HSCollectionVC.h"
#import "HSListModel.h"
#import "HSListCell.h"
#import "HSListDetailVC.h"

@interface HSCollectionVC ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray<HSListModel *> *topics;

@end

@implementation HSCollectionVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"我的收藏";
    [self.view addSubview:self.tableView];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSArray *data = [[MYCacheHelper shareHelper] my_cacheWithKey:collectionKey clazz:[NSArray class]];
    if (data && data.count > 0) {
        self.topics = data.mutableCopy;
    }
    else{
        self.topics = @[].mutableCopy;
    }
    [self.tableView reloadData];
    self.hintView.hintState = MYHintStateLoading;
    @weakify(self);
    [[GCDQueue mainQueue] queueBlock:^{
        @strongify(self);
        self.hintView.hintState = self.topics.count > 0 ? MYHintStateHidden : MYHintStateNoData;
    } afterDelay:1.5];
}

#pragma mark - UITableView Delegate & Datasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.topics.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuseId = @"HSListCell";
    HSListCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
    if (!cell) {
        cell = [[HSListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseId];
    }
    cell.model = self.topics[indexPath.row];
    [cell showLineForDirection:MYDirectionBottom];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 85;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    [self my_push:[[HSListDetailVC alloc] initWithModel:self.topics[indexPath.row]]];
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height - NavBarHeight - TabbarHeight)];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
    }
    return _tableView;
}

@end
